<template>
  <div class="login_container">
    <div class="box">
      <h2 style="color: rgb(249,231,172);">欢迎登录</h2>
      <!-- 登录表单 -->
      <el-form
          :model="form"
          :rules="rules" ref="loginForm"
          label-width="0px"
          class="login_form">
        <el-form-item prop="username">
          <el-input
              v-model="form.username"
              prefix-icon="el-icon-user"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="form.password"
              prefix-icon="el-icon-key"
              placeholder="请输入密码" type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="validCode">
          <div style="display: flex">
            <el-input
                v-model="form.validCode" style="width: 50%"
                prefix-icon="el-icon-lock" @keydown.enter.native = "login"
                placeholder="请输入验证码"></el-input>
            <!--  这里不能带()，因为这是个有参函数，直接写函数名就可以了-->
            <ValidCode @input="getValidCode" style="margin-left: 60px" ref="child" class="unselectable"></ValidCode>
          </div>
        </el-form-item>
        <el-form-item class="btn-box">
          <el-button type="primary" class="btn" @click="login()">登录</el-button>
          <el-button type="info" @click="$router.push('/register')" class="btn">注册</el-button>
          <el-button type="primary" class="btn" @click="jump()">管理员登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode.vue";
import request from "@/utils/request";

export default {
  name: "Login",
  created() {
    document.title = '欢迎登录';
  },
  components:{
    ValidCode,
  },
  data() {
    return {
      form: {
        username:'',
        password: ''
      },
      validCode: "",
      // 表单数据验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名！", trigger: "blur" },
          {
            min: 2,
            max: 25,
            message: "长度在 2 到 25 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码！", trigger: "blur" },
          { min: 1, max: 15, message: "长度在 1到 15 个字符", trigger: "blur" },
        ],
        validCode: [
          { required: true, message: "请输入验证码！", trigger: "blur" }
        ],
      },
    }
  },
  methods: {
    // 从验证码子组件里面获取正确的验证码
    getValidCode(data){
      this.validCode = data;
    },
    login() {
      this.$refs['loginForm'].validate((valid) => {
        // 如果满足输入的规则
        if (valid){
          // 验证码不正确
          if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()){
            this.$message.error('验证码错误');
            // 更新验证码
            this.$refs.child.refreshCode();
            return;
          }
          // 出来了就是验证码正确，就发送给后端
          request.post('/user/login', this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: 'success',
                message: "登录成功",
              });
              // 缓存用户信息
              sessionStorage.setItem("user", JSON.stringify(res.data));
                this.$router.push('/shop');
            } else {
              this.$message({
                type: 'error',
                message: res.msg,
              })
            }
          });
        }
      });
    },
    jump(){
      this.$router.push('/manageLogin');
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("@/assets/images/login.jpg") no-repeat;
  background-size: cover;
}
.btn-box {
  width: 400px; // 按钮容器宽度
  display: flex; // 使用flex布局
  justify-content: center; // 水平居中
  align-items: center; // 垂直居中
}

.btn {
  width: 120px; // 按钮宽度设置为100%，以适应容器宽度
  height: 35px;
  border: 1px solid rgba(197, 81, 58, 0.8);
  background: rgba(197, 81, 58, 0.5);
  color: rgba(255, 255, 255, 0.9);
  border-radius: 5px;
  transition: 0.2s;
  cursor: pointer;
}

.box {
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 600px;
  height: 400px;
  backdrop-filter: blur(10px);
  background: rgba(50, 50, 50, 0.2);
}

.box > h2 {
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 30px;
  font-size: 30px;
}

.unselectable {
  -webkit-user-select: none; /* Safari */
  -moz-user-select: none;    /* Firefox */
  -ms-user-select: none;     /* IE10+/Edge */
  user-select: none;         /* Standard */
}
</style>
